package com.wenjiang.controller;


import com.wenjiang.common.annotation.RequireToken;
import com.wenjiang.common.annotation.TokenUserId;
import com.wenjiang.common.dto.AddressParam;
import com.wenjiang.common.lang.Result;
import com.wenjiang.service.MallUserAddressService;
import com.wenjiang.common.util.JWTUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lwj
 * @since 2021-07-20
 */
@RestController
@RequestMapping("/mallUserAddress")
public class MallUserAddressController {

    @Autowired
    MallUserAddressService mallUserAddressService;


    //获取用户的收获信息
    @RequireToken
    @RequestMapping ("/getUserAddress")
    public  Object getUserAddress(HttpServletRequest request){

        String token = request.getHeader("token");

        Long userId = JWTUtil.getUserId(token);

        return  mallUserAddressService.getAddressById(userId);
    }

    //修改或添加用户的信息
    @RequestMapping("/edit")
    @RequireToken
    public  Object updateOrSaveAddress(@RequestBody AddressParam addressParam , @TokenUserId Long userId){
        Result result = mallUserAddressService.saveOrUpdate(addressParam, userId);
        return  result;
    }

    //删除用户地址信息
    @RequestMapping("/del/{addressId}")
    @RequireToken
    public  Object delAddress(@PathVariable(name = "addressId") Long addressId){
        Result result = mallUserAddressService.del(addressId);

        return result;
    }
}
